package com.hly.baseactivity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding

abstract class BaseActivity<VB:ViewBinding,VM: ViewModel> :AppCompatActivity() {
    private var _binding: VB? = null
    protected val binding: VB get() = _binding!!
    protected lateinit var viewModel: VM

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel = ViewModelProvider(viewModelStore, ViewModelProvider.NewInstanceFactory())[initViewModel()]
        _binding = initViewBinding()
        setContentView(binding.root)
        initData()
        initView(savedInstanceState)

    }
    override fun onDestroy() {
        super.onDestroy()
        _binding = null
    }

    abstract fun initViewBinding(): VB
    abstract fun initData()
    abstract fun initView(savedInstanceState: Bundle?)
    abstract fun initViewModel(): Class<VM>

}